package com.example.permission.controller;

import com.example.permission.common.page.Page;
import com.example.permission.common.result.Result;
import com.example.permission.entity.dto.SysGroupDTO;
import com.example.permission.entity.dto.page.SysGroupPageDTO;
import com.example.permission.entity.po.SysGroup;
import com.example.permission.service.SysGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 组织机构
 *
 * @author xuwq
 * @date 2020-05-09 15:56:07
 */
@RestController
@Api(tags = "组织机构")
public class SysGroupController {

    @Autowired
    private SysGroupService sysGroupService;

    @PostMapping("v1/group")
    @ApiOperation("新增")
    public Result<SysGroup> add(@RequestBody @Valid SysGroupDTO sysGroupDTO) {
        return Result.success(this.sysGroupService.insert(sysGroupDTO));
    }

    @PutMapping("v1/group/{groupId}")
    @ApiOperation("修改")
    public Result<SysGroup> edit(@PathVariable("groupId") Integer groupId, @RequestBody @Valid SysGroupDTO sysGroupDTO) {
        sysGroupDTO.setGroupId(groupId);
        return Result.success(this.sysGroupService.update(sysGroupDTO));
    }

    @DeleteMapping("v1/group/{groupId}")
    @ApiOperation("删除")
    public Result<Boolean> deleteById(@PathVariable("groupId") Integer groupId) {
        return Result.success(this.sysGroupService.deleteById(groupId));
    }

    @GetMapping("v1/group/{groupId}")
    @ApiOperation(value = "根据id查询数据")
    public Result<SysGroup> queryById(@PathVariable("groupId") Integer groupId) {
        return Result.success(this.sysGroupService.queryById(groupId));
    }

    @PostMapping("v1/group/multi")
    @ApiOperation(value = "查询列表")
    public Result<Page<SysGroup>> queryListByPage(@RequestBody SysGroupPageDTO sysGroupPageDTO) {
        return Result.success(this.sysGroupService.queryListByPage(sysGroupPageDTO));
    }

}